package com.chasing.baseframe.widgets.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import net.chasing.androidbaseconfig.util.GenericTools;
import net.chasing.androidbaseconfig.util.OneClickUtil;

/**
 * 因为继承BaseDialog之后，需要在构造方法的第一行进行调用super()，
 * 而initView又是在super()内进行调用初始化了
 * 所以子类想要通过构造方法传值改变initView的初始化，
 * 只能通过Intent形式传递进来，再进行读取使用
 * 例：
 * public UpdateDialog(@NonNull Context context, boolean isForce) {
 *     super(context, new Intent().putExtra("isForce", isForce));
 * }
 */
public abstract class BaseDialog extends Dialog {

    protected Context mContext;
    protected int width, height;
    protected boolean isFullScreen, isTransparent;
    protected OneClickUtil mOneClickUtil;
    protected Intent mIntent;

    public BaseDialog(@NonNull Context context) {
        super(context, androidx.appcompat.R.style.Theme_AppCompat_Light_Dialog_Alert);
        init(context);
    }

    public BaseDialog(@NonNull Context context, Intent intent) {
        super(context, androidx.appcompat.R.style.Theme_AppCompat_Light_Dialog_Alert);
        mIntent = intent;
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        View view = View.inflate(context, getLayoutId(), null);
        setContentView(view);
        mOneClickUtil = new OneClickUtil();

        initView(view);

        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(net.chasing.androidbaseconfig.R.color.transparent);
            if (width == 0)
                width = (int) (GenericTools.getScreenWidth((Activity) context) -
                        context.getResources().getDimension(net.chasing.androidbaseconfig.R.dimen.dialog_width_space));
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = width;
            if (height != 0)
                attributes.height = height;
            attributes.windowAnimations = net.chasing.androidbaseconfig.R.style.AnimBottomDialog;
            window.setAttributes(attributes);
        }

        if (isFullScreen)
            setFullScreen();
        else if (isTransparent)
            setTransparentStatusBar();
    }

    public void setFullScreen() {
        Window window = getWindow();
        //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
        if (window != null) {
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            window.getDecorView().setSystemUiVisibility(option);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            //设置导航栏颜
            window.setNavigationBarColor(Color.TRANSPARENT);

            //适配刘海屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                WindowManager.LayoutParams localLayoutParams = window.getAttributes();
                localLayoutParams.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            }
        }
    }

    public void setTransparentStatusBar() {
        Window window = getWindow();
        if (window != null) {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.setStatusBarColor(ContextCompat.getColor(mContext, android.R.color.transparent));
        }
    }

    public abstract int getLayoutId();

    public abstract void initView(View view);

    @Override
    public void show() {
        if (isFullScreen)
            setFullScreen();
        else if (isTransparent)
            setTransparentStatusBar();
        super.show();
    }
}
